import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ken
 * Date: 2024-02-03
 * Time: 16:17
 */
public class Test {
    public int size(){
        int count = 0;
        Node cur = head;
        while (cur != null) {
            count++;
            System.out.println(cur.val);
            cur = cur.next;

        }


        return count;
    }
    public void clear() {
        Node cur = this.head;
        while(cur != null) {
            cur.val = null;
        }
        return;
    }
   public ListNode reverseList() {
        if(head == null) {
            return null;
        }
        if(head.next == null) {
            return head;
        }
       ListNode cur = head.next;
       head.next = null;
        while(cur != mull) {
            ListNode tmp = cur.next;
            cur.next = head;
            head = cur;
            cur = tmp;
        }
        return head;
   }
   public ListNode middleNode() {
        Node cur = this.head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        if(count == 2) {
            return head.next;
        }
        Node tmp = this.head;
        int in = 0;
        while(tmp != null) {
            if(in == count / 2) {
                return tmp;
            }
            tmp = tmp.next;
            in++;
        }

   }
    public ListNode middleNode() {
        ListNode fast = this.head;
        ListNode slow = this.head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;

        }
        return slow;

    }
    public ListNode find(int k) {
        if(k <= 0 || head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        for (int i = k - 1; i > 0 ; i--) {
            fast = fast.next;
            if(fast == null) {   //处理K太大的问题
                return null;
            }
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    public boolean chkPalindrome(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        //1.找到中间的位置
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2.翻转
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        while (head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            if(head.next == cur) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }
    public ListNode getIntersectionNode(ListNode headA,ListNode headB) {
        int count1 = 0;
        int count2 = 0;
        while (headA != null){
            count1++;
            headA = headA.next;
        }
        while (headB != null){
            count2++;
            headA = headB.next;
        }
        int cha = 0;
        if(count1 > count2) {
            cha = count1 - count2;
            for (int i = cha; i > 0 ; i--) {
                headA = headA.next;
            }
        } else {
           cha = - (count2 - count1);
            for (int i = cha; i > 0 ; i--) {
                headA = headB.next;
            }
        }
        while (headA != headB) {
            headA = headA.next;
            headB = headB.next;
        }
        if(headA == null) {
            return null;
        }
        return headA;
    }
}
